package zhao.tx.eecs.AbsActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.andware.volley.RequestQueue;
import com.andware.volley.VolleyError;

import java.util.Random;

import zhao.tx.eecs.MyVolley;
import zhao.tx.eecs.Tools.VolleyBase;

//import android.support.v7.app.ActionBar;
//import android.support.v7.app.ActionBarActivity;

/**
 * Created by ishishuji on 14-8-19.
 */
public abstract class BaseActivity extends Activity implements IwebListener{

    private Context context;

//    public ActionBar actionBar;

    private RequestQueue requestQueue;

    private int myProgress = 0;

    private Random random = new Random();

    private boolean isWaiting = true;

    private IwebListener iwebListener;

    private Handler webHandler = new Handler (  ) {

        @Override
        public void handleMessage ( Message msg ) {
            super.handleMessage ( msg );
            switch ( msg.what ) {
                case VolleyBase.SUCCESS:
                    isWaiting = false;
                    iwebListener.webConSuccessed ( msg.obj );
                    break;
                case VolleyBase.FAILD:
                    isWaiting = false;
                    iwebListener.webConFailed ( ( VolleyError ) msg.obj );
                    break;
            }
        }
    };

    private Runnable webLoading = new Runnable () {
        @Override
        public void run () {
            while ( isWaiting ) {
                myProgress += 10;
                iwebListener.webConWaitting ( myProgress );
                try {
                    Thread.sleep ( random.nextInt(1000) );
                } catch ( InterruptedException e ) {
                    e.printStackTrace ();
                }
            }
        }
    };

    public void setIwebListener ( IwebListener listener ) {
        this.iwebListener = listener;
    }

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );

//        actionBar = getActionBar ( this );

        requestQueue = MyVolley.getRequestQueue ();

        VolleyBase.setBaseQueue ( requestQueue );

        setIwebListener ( this );

        setContext ( this );
    }

//    private ActionBar getActionBar(Activity activity) {
//        if (activity instanceof ActionBarActivity) {
//            return ((ActionBarActivity ) activity).getSupportActionBar();
//        }
//        ActionBar actionBar = getActionBarWithReflection(activity, "getSupportActionBar");
//        if (actionBar == null) {
//            throw new RuntimeException("Activity should derive from ActionBarActivity "
//                    + "or implement a method called getSupportActionBar");
//        }
//        return actionBar;
//    }
//
//    protected <T> T getActionBarWithReflection(Activity activity, String methodName) {
//        try {
//            Method method = activity.getClass().getMethod(methodName);
//            return (T)method.invoke(activity);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (ClassCastException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public Context getContext () {
        return context;
    }

    public void setContext ( Context context ) {
        this.context = context;
    }

    public void onThisFinish () {
        this.finish ();
    }

    @Override
    protected void onStop () {
        super.onStop ();
        VolleyBase.cancelAll ( this );
    }

    public RequestQueue getRequestQueue () {
        return requestQueue;
    }

    @Override
    protected void onPause () {
        super.onPause ();
        VolleyBase.cancelAll ( this );
    }

    @Override
    protected void onResume () {
        super.onResume ();

    }

    public Handler getWebHandler () {
        return webHandler;
    }

    public void setWebHandler ( Handler webHandler ) {
        this.webHandler = webHandler;
    }

    public int getMyProgress () {
        return myProgress;
    }

    public void setMyProgress ( int myProgress ) {
        this.myProgress = myProgress;
    }

    public Runnable getWebLoading () {
        return webLoading;
    }

    public void setWebLoading ( Runnable webLoading ) {
        this.webLoading = webLoading;
    }


}
